<?php

namespace App\Http\Controllers\Manage;


use App\Models\User;
use App\Models\UserAuth;

class StudentController extends BaseController
{

    // 用户
    public function initialize()
    {
        parent::initialize();

        $this->model = 'Student';
    }


    // 列表
    // 添加
    // 修改
    // 删除


    // 列表数据展示
    protected function listTable($lists)
    {
        // 整理
        $trList = [];
        if(empty($lists)){
            return $trList;
        }

        // 获取用户信息
        $uids  = array_column($lists, 'uid');
        $users = (new User())->isCache(true)->getListInIds($uids);
        // 获取用户信息
        $aids  = array_column($lists, 'auth_id');
        $auths = (new UserAuth())->isCache(true)->getListInIds($aids);

        // 展示列表表格
        $sexes    = [1 => '男', 2 => '女', 0 => '保密'];
        $listHtml = $this->adminList();
        foreach($lists as $key => $item){
            // 数据
            $_id      = $item['id'];
            $status   = $item['status'] == 1 ? '正常' : '禁用';
            $statusOp = $item['status'] == 1 ? '禁用' : '启用';
            // 操作
            $operate = '';
            //$operate .= $listHtml->operate_status(['id' => $_id, 'value' => 1 - $item['status']], $statusOp, $item['status']);
            $operate .= $listHtml->operate_edit(['id' => $_id], '编辑学员');
            $operate .= $listHtml->operate_delete(['id' => $_id], '你确定要删除该学员吗？');

            // 页面
            $tr = $listHtml->list_check($item['id']);
            $tr .= $listHtml->list_td($item['id']);
            //$tr .= $listHtml->list_td($users[$item['uid']]['nickname'] ?? '-');
            $tr .= $listHtml->list_td($auths[$item['auth_id']]['auth_name'] ?? '-');
            $tr .= $listHtml->list_td($item['person_uid'] ?: '');
            $tr .= $listHtml->list_td($item['person_name']);
            $tr .= $listHtml->list_td($sexes[$item['sex']] ?? '-');
            $tr .= $listHtml->list_td($item['age']);
            $tr .= $listHtml->list_td($item['guardor']);
            $tr .= $listHtml->list_td($item['mobile']);
            //$tr .= $listHtml->list_td($status);
            $tr .= $listHtml->list_td(date('Y-m-d H:i', $item['ctime']));
            $tr .= $listHtml->list_td($operate);

            // 整合
            $trList[] = $listHtml->list_tr($tr);
        }

        // 整合
        return $trList;
    }

    // 列表顶部按钮
    protected function listTopButton()
    {
        $addUrl     = Url('/manage/Student/add');
        $top_button = '<a href="javascript:;" onclick="onEdit(\''.$addUrl.'\')" class="btn btn-success radius">添加学员</a> ';
        $this->assign('top_button', $top_button);
    }

    // 列表字段
    protected function list_fields()
    {
        return ['id' => 'ID', 'uid' => '所属认证机构', 'person_uid' => '接入用户ID', 'person_name' => '姓名', 'sex' => '性别', 'age' => '年龄', 'guardor' => '监护人', 'mobile' => '手机号', 'ctime' => '创建时间', 'operate' => '操作',];
    }

    // 列表搜索
    protected function list_search()
    {
        $request = $this->optLogic()->getData();
        $form    = $this->adminList();

        $form_uid    = $form->formInput('form_uid', $request['form_uid'] ?? null, '从用户id', '120px');
        $keyword     = $form->formInput('keyword', $request['keyword'] ?? '', '关键字');
        $person_name = $form->formInput('person_name', $request['person_name'] ?? '', '姓名');
        $mobile      = $form->formInput('mobile', $request['mobile'] ?? '', '手机号');
        $search      = ['form_uid' => $form_uid, 'mobile' => $mobile, 'person_name' => $person_name,];
        $this->assign('search', $search);

        $searchHide = ['channel'];
        $this->assign('searchHide', []);
    }

    // 表单页面
    protected function formTable()
    {
        $manage = $this->checkManage($this->model);
        if($manage === false){
            return $this->error($manage->getError());
        }

        $html = '';
        $info = $manage->onShow();
        $form = $this->adminForm();

        // 用户
        $uInfo  = (new User())->isCache(true)->getOneById($info['uid'] ?? 0);
        $auInfo = (new UserAuth())->isCache(true)->getOneById($info['auth_id'] ?? 0);

        // 用户名
        //$role     = $form->formCheckbox('role', json_decode($info['role']), $all); // 角色
        //$status   = $form->formRadio('status', $info['status'] ?? 1, [1 => '正常', 0 => '禁用']);
        //$uid         = $form->ajaxSearch('uid', $info['uid'] ?? '', 'User');
        $uid         = $form->ajaxSingleFromList('uid', $uInfo, 'User', 'id', 'username');
        $auth_id     = $form->ajaxSingleFromList('auth_id', $auInfo, 'Auth', 'id', 'auth_name');
        $person_name = $form->formInput('person_name', $info['person_name'] ?? '', '姓名');
        $head_img    = $form->formImage('head_img', $info['head_img'] ?? '');
        $sex         = $form->formRadio('sex', $info['sex'] ?? 1, [1 => '男', 2 => '女', 0 => '保密']);
        $age         = $form->formNumber('age', $info['age'] ?? '', '年龄');
        $guardor     = $form->formInput('guardor', $info['guardor'] ?? '', '监护人');
        $mobile      = $form->formInput('mobile', $info['mobile'] ?? '', '手机号');
        $_id         = $form->formHidden('id', $info['id'] ?? '');

        //$html     .= $form->makeItem('确认密码', $password, '添加时候不填默认密码123456，修改不填默认不修改');
        //$html .= $form->makeItem('所属认证机构', $uid);
        $html .= $form->makeItem('所属认证机构', $auth_id);
        $html .= $form->makeItem('姓名', $person_name);
        $html .= $form->makeItem('头像', $head_img);
        $html .= $form->makeItem('性别', $sex);
        $html .= $form->makeItem('年龄', $age);
        $html .= $form->makeItem('监护人', $guardor);
        $html .= $form->makeItem('手机号', $mobile);
        $html .= $_id;

        return $html;
    }

}


